/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package multiface.valentinorusso.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import multiface.valentinorusso.jpa.util.PersistenceManager;
import multiface.valentinorusso.jpa.Utente;
import it.sauronsoftware.base64.Base64;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
 *
 * @author Diego64
 */
@WebServlet(name = "LoginServlet", urlPatterns = {"/LoginServlet"})
public class LoginServlet extends HttpServlet {

    private static Log log = LogFactory.getLog(LoginServlet.class);
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        HttpSession s = request.getSession();
        try {
            Thread.sleep(500);
            String psw64 = Base64.encode(request.getParameter("password"));
            Utente u = (Utente) em.createNativeQuery("SELECT * FROM utente WHERE username=? AND password=? AND tipo=?", Utente.class).setParameter(1, request.getParameter("user")).setParameter(2,psw64).setParameter(3, "AMMINISTRATORE").getSingleResult();
            if (u != null) {
                s.setAttribute("admin", u);
                
                multiface.valentinorusso.jpa.Log l = new multiface.valentinorusso.jpa.Log();
                l.setDataora(new java.util.Date());
                l.setLog("login");
                l.setNominativo(u.getNome() + " " + u.getCognome());
                
                em.getTransaction().begin();
                em.persist(l);
                em.getTransaction().commit();   
                
                
                out.print(s.getId());
            } else {
                out.print("no");
            }
        } catch (Exception ex) {
            out.print("no"+ex);
            log.error(ex);
        } finally {
            out.close();
            em.close();
            //emf.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
